/*
 * Copyright (C) 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.javielinux.pruebadata;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.android.dataframework.DataFramework;
import com.android.dataframework.Entity;

public class NoteEdit extends Activity {

	private EditText mTitleText;
    private EditText mBodyText;
    private Spinner mCategory;
    Entity currentEntity;
    List<Entity> categories;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      
        setContentView(R.layout.note_edit);
       
        mTitleText = (EditText) findViewById(R.id.title);
        mBodyText = (EditText) findViewById(R.id.body);
        mCategory = (Spinner) findViewById(R.id.category);
      
        categories = DataFramework.getInstance().getEntityList("categories");
        ArrayAdapter<Entity> adapter = new ArrayAdapter<Entity>(this,
                android.R.layout.simple_spinner_item, categories);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mCategory.setAdapter(adapter);
        
        Button confirmButton = (Button) findViewById(R.id.confirm);
       
        long mRowId = savedInstanceState != null ? savedInstanceState.getLong(DataFramework.KEY_ID) : -1;
		if (mRowId < 0) {
			Bundle extras = getIntent().getExtras();            
			mRowId = extras != null ? extras.getLong(DataFramework.KEY_ID) : -1;
		}

		currentEntity = new Entity("notes", mRowId);
		
		populateFields();
        confirmButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                currentEntity.setValue("title", mTitleText.getText().toString());
                currentEntity.setValue("body", mBodyText.getText().toString());
                currentEntity.setValue("category_id", ((Entity)mCategory.getSelectedItem()).getId());
                currentEntity.save();
                setResult(RESULT_OK);
                finish();
            }
          
        });
    }
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putLong(DataFramework.KEY_ID, currentEntity.getId());
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        saveState();
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        populateFields();
    }
    
    private void saveState() {
    }
    
    private void populateFields() {
        if (currentEntity.isUpdate()) {
            mTitleText.setText(currentEntity.getValue("title").toString());
            mBodyText.setText(currentEntity.getValue("body").toString());
            mCategory.setSelection(DataFramework.getInstance().getPosition((ArrayList<Entity>)categories, currentEntity.getEntity("category_id").getId()));
            
        }
    }
    
}
